package com.example.administrator.tenyuanpackage.speacial.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.tenyuanpackage.R;
import com.example.administrator.tenyuanpackage.speacial.bean.TenBean;

import java.util.List;

/**
 * Created by yq on 2017/2/28.
 */

public class TenRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context  context;
    private List<TenBean.RowsBean> list;
    public TenRecyclerViewAdapter(Context  context, List<TenBean.RowsBean> list){
        this.context=context;
        this.list=list;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_goods, parent, false);

        return  new MyViewHouder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if(holder instanceof MyViewHouder){
            TenBean.RowsBean rowsBean = list.get(position);

            Glide.with(context).load(rowsBean.getProductImg()).into(((MyViewHouder) holder).ivGoods);

            //设置商品名字
            ((MyViewHouder) holder).tvName.setText(rowsBean.getName());
            //处理新价格的小数点，保留两位
            double newPrice = ((int) rowsBean.getNewPrice() * 100) / 100;
            //设置新价格
            ((MyViewHouder) holder).tvNewPrice.setText("￥" + newPrice);
            //处理旧价格的小数点，保留两位
            double oldPrice = ((int) rowsBean.getOldPrice() * 100) / 100;
            ((MyViewHouder) holder).tvOldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
            //折扣
            double discount = 0;
            //如果旧价格为0，则设为0.00
            if (oldPrice == 0) {
                ((MyViewHouder) holder).tvOldPrice.setText("0.00");
                discount = 0.00;
            } else {
                ((MyViewHouder) holder).tvOldPrice.setText(oldPrice + "");
                discount = newPrice * 10 / oldPrice;
                discount = ((int)discount * 100) / 100;
            }
            //设置折扣
            ((MyViewHouder) holder).tvDiscount.setText(discount + "折");
            //设置已售商品件数
            ((MyViewHouder) holder).tvSaleTotal.setText("已售" + rowsBean.getSaleTotal() + "件");


        }


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

   public   class MyViewHouder extends RecyclerView.ViewHolder {

       private ImageView ivGoods;
       private TextView tvName, tvNewPrice, tvOldPrice, tvDiscount, tvSaleTotal;

        public MyViewHouder(View itemView) {
            super(itemView);

            ivGoods = (ImageView) itemView.findViewById(R.id.iv_goods);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvNewPrice = (TextView) itemView.findViewById(R.id.tv_new_price);
            tvOldPrice = (TextView) itemView.findViewById(R.id.tv_old_price);
            tvDiscount = (TextView) itemView.findViewById(R.id.tv_discount);
            tvSaleTotal = (TextView) itemView.findViewById(R.id.tv_sale_total);
        }
    }
}
